package com.HQCourse.wechat.wechat.service.impl;

import com.HQCourse.Exception.HQCourseException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.HQCourse.model.wechat.Menu;
import com.HQCourse.wechat.wechat.mapper.MenuMapper;
import com.HQCourse.wechat.wechat.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.HQCourse.vo.wechat.MenuVo;
import com.sun.javafx.runtime.async.BackgroundExecutor;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单明细 订单明细 服务实现类
 * </p>
 *
 * @author zww
 * @since 2023-07-04
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private WxMpService wxMpService;

    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            throw new HQCourseException(20001,"删除失败");
        }
    }

    //同步菜单

    /**
     * 说明：
     * 自定义菜单最多包括3个一级菜单，每个一级菜单最多包含5个二级菜单。
     * 一级菜单最多4个汉字，二级菜单最多8个汉字，多出来的部分将会以“...”代替。
     * 创建自定义菜单后，菜单的刷新策略是，在用户进入公众号会话页或公众号profile页时，
     * 如果发现上一次拉取菜单的请求在5分钟以前，就会拉取一下菜单，如果菜单有更新，就会刷新客户端的菜单。测试时可以尝试取消关注公众账号后再次关注，则可以看到创建后的效果。
     */
    @SneakyThrows
    @Override
    public void syncMenu() {
        List<MenuVo> menuVoList = this.findMenuInfo();
        //菜单
        JSONArray buttonList = new JSONArray();
        for(MenuVo oneMenuVo : menuVoList) {
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            JSONArray subButton = new JSONArray();
            for(MenuVo twoMenuVo : oneMenuVo.getChildren()) {
                JSONObject view = new JSONObject();
                view.put("type", twoMenuVo.getType());
                if(twoMenuVo.getType().equals("view")) {
                    view.put("name", twoMenuVo.getName());
                    view.put("url", "https://7f6653785e.goho.co/#"
                            +twoMenuVo.getUrl());
                } else {
                    view.put("name", twoMenuVo.getName());
                    view.put("key", twoMenuVo.getMeunKey());
                }
                subButton.add(view);
            }
            one.put("sub_button", subButton);
            buttonList.add(one);
        }
        //菜单
        JSONObject button = new JSONObject();
        button.put("button", buttonList);
        try {
            this.wxMpService.getMenuService().menuCreate(button.toJSONString());
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new HQCourseException(20001,"公众号菜单同步失败");
        }
    }

    @Override
    public List<MenuVo> findMenuInfo() {
        List<MenuVo> list=new ArrayList<>();
        List<Menu> list1 = baseMapper.selectList(null);
        //获取一级菜单数据
        List<Menu> list2=list1.stream().filter(menu ->
            menu.getParentId().longValue()==0)
                .collect(Collectors.toList());
        //获取二级菜单信息
         for (Menu menu:list2){
             MenuVo menuVo=new MenuVo();
             BeanUtils.copyProperties(menu,menuVo);
             List<Menu> list3=list1.stream().filter(menu1 ->
                             menu1.getParentId().longValue()==menu.getId())
                             .collect(Collectors.toList());
             //封装二级菜单信息到一级菜单里面
               List<MenuVo> list4=new ArrayList<>();
               for (Menu menu1:list3){
                   MenuVo menuVo1=new MenuVo();
                   BeanUtils.copyProperties(menu1,menuVo1);
                   list4.add(menuVo1);
               }
               menuVo.setChildren(list4);
               list.add(menuVo);
         }
        return list;
    }
    //查询一级菜单
    @Override
    public List<Menu> findOneMenuInfo() {
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<Menu> list = baseMapper.selectList(wrapper);
        return list;
    }
}
